package com.runner.utility;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.runner.RedRunner;
import com.runner.device.Device;
import com.runner.observer.Event;
import com.runner.observer.Subject;
import com.runner.services.AccomplishmentsOutbox;
import com.runner.services.Achievements;
import com.runner.services.GoogleServices;
import com.runner.sound.ConsoleAudio;
import com.runner.sound.IAudio;

public class Locator implements Constants {
	private static IAudio audioService;
	private static ArrayList<ParticleEffect> effects;
	private static InputMultiplexer multi;
	private static RedRunner game;
	private static World world;
	private static float xSpriteScale, ySpriteScale;
	private static FitViewport viewport;
	public static float xGameSpeed, yGameSpeed;
	public static float xHardGameSpeed, yHardGameSpeed, xEasyGameSpeed, yEasyGameSpeed;
	public static float overallGameSpeed;
	public static int difficulty;
	public static int coins;
	public static boolean mute;
	public static Assets assets;
	public static TimedEventManager events;
	public static Achievements achvs;
	public static AccomplishmentsOutbox accomplishments;
	public static Device device;
	public static GoogleServices services;
	public static Camera camera;
	public static Unlocks unlocks;
	public static int playAdCounter;

	public static final Color COLOR_RINGER_TEXT = new Color(0.772f, 0.561f, 0.361f, 1);
	public static final Color COLOR_RINGER_LIGHT_TEXT = new Color(0.816f, 0.957f, 0.969f, 1);
	public static final Color COLOR_APRIL_TEXT = new Color(0.2f, 1f, 0.2f, 1);
	public static final Color COLOR_APRIL_LIGHT_TEXT = new Color(0.816f, 0.957f, 0.969f, 1);
	public static final Color COLOR_GEO_TEXT = new Color(0.772f, 0.561f, 0.361f, 1);
	public static final Color COLOR_GEO_LIGHT_TEXT = new Color(0.816f, 0.957f, 0.969f, 1);
	public static int HARD = 1;
	public static int EASY = 0;

	public static void init(RedRunner g, Device d, GoogleServices s, IAudio audio, Camera c, FitViewport view) {
		achvs = new Achievements();
		accomplishments = new AccomplishmentsOutbox(achvs);
		camera = c;
		unlocks = new Unlocks();
		effects = new ArrayList<ParticleEffect>();
		multi = new InputMultiplexer();
		game = g;
		device = d;
		services = s;
		playAdCounter=PLAYS_BEFORE_AD;
		audioService = new ConsoleAudio();
		events = new TimedEventManager();
		xSpriteScale = 1;
		ySpriteScale = 1;
		overallGameSpeed = 1f;
		difficulty = HARD;
		assets = new Assets();
		assets.loadAssets();
		audioService = audio;
		viewport = view;
		Locator.readSave();

	}

	public static void setSpriteScale(float x_scale, float y_scale) {
		Locator.setEasyGameSpeed(1,1);
		Locator.setHardGameSpeed(1,1);
		Locator.useDifficultySpeed();
		xSpriteScale = x_scale;
		ySpriteScale = y_scale;
	}

	public static void scheduleEvent(float time, Event event, Subject subject) {
		events.scheduleEvent(time, event, subject);
	}

	public static void setDifficutly(int d) {
		difficulty = d;
		Locator.useDifficultySpeed();
	}

	public static void setWorld(World w) {
		world = w;
	}

	public static AssetManager getAssets() {
		return assets.getManager();
	}

	public static void win() {
		writeSave();
		audioService.stopMusic();
	}

	public static void readSave() {
		Preferences prefs = Gdx.app.getPreferences("My Preferences");
		mute = prefs.getBoolean("mute", false);
		coins = prefs.getInteger("coins", 0);
		accomplishments.loadLocal(prefs);
		unlocks.loadLocal(prefs);
	}

	public static void writeSave() {
		Preferences prefs = Gdx.app.getPreferences("My Preferences");
		prefs.putBoolean("mute", mute);
		prefs.putInteger("coins", coins);
		accomplishments.saveLocal(prefs);
		unlocks.saveLocal(prefs);
		prefs.flush();
	}
	
	public static void increaseAdCounter(){
		playAdCounter++;
	}
	
	public static int getAdCounter(){
		return playAdCounter;
	}
	
	public static void resetAdCounter(){
		playAdCounter=0;
	}

	public static Achievements getAchvs() {
		return achvs;
	}

	public static AccomplishmentsOutbox getAccomplishments() {
		return accomplishments;
	}

	public static void setMute(boolean m) {
		mute = m;
	}

	public static Unlocks getUnlocks() {
		return unlocks;
	}

	public static World getWorld() {
		return world;
	}

	public static Device getDevice() {
		return device;
	}

	public static GoogleServices getServices() {
		return services;
	}

	public static InputMultiplexer getInputMultiplexer() {
		return multi;
	}

	public static IAudio getAudio() {
		return audioService;
	}

	public static ArrayList<ParticleEffect> getEffects() {
		return effects;
	}

	public static RedRunner getGame() {
		return game;
	}

	public static TimedEventManager getEvents() {
		return events;
	}

	public static void setEvents(TimedEventManager events) {
		Locator.events = events;
	}

	public static boolean isMute() {
		return mute;
	}

	public static float getxSpriteScale() {
		return xSpriteScale;
	}

	public static float getySpriteScale() {
		return ySpriteScale;
	}

	public static FitViewport getViewport() {
		return viewport;
	}

	public static Camera getCamera() {
		return camera;
	}

	public static void setCamera(Camera camera) {
		Locator.camera = camera;
	}

	public static float getXGameSpeed() {
		return xGameSpeed;
	}

	public static float getYGameSpeed() {
		return yGameSpeed;
	}

	public static void setGameSpeed(float x_gameSpeed, float y_gameSpeed) {
		Locator.xGameSpeed = 2 * x_gameSpeed;
		Locator.yGameSpeed = 2 * y_gameSpeed;
	}

	public static void setEasyGameSpeed(float x_gameSpeed, float y_gameSpeed) {
		Locator.xEasyGameSpeed = 2 * x_gameSpeed;
		Locator.yEasyGameSpeed = 2 * y_gameSpeed;
	}

	public static void setHardGameSpeed(float x_gameSpeed, float y_gameSpeed) {
		Locator.xHardGameSpeed = 2 * x_gameSpeed;
		Locator.yHardGameSpeed = 2 * y_gameSpeed;
	}

	public static void useEasySpeed() {
		Locator.xGameSpeed = Locator.xEasyGameSpeed;
		Locator.yGameSpeed = Locator.yEasyGameSpeed;
	}

	public static void useHardSpeed() {
		Locator.xGameSpeed = Locator.xHardGameSpeed;
		Locator.yGameSpeed = Locator.yHardGameSpeed;
	}

	public static void setOverallGameSpeed(float i) {
		overallGameSpeed = Math.max(0, i);

	}

	public static float getOverallGameSpeed() {
		return overallGameSpeed;
	}

	public static void useDifficultySpeed() {
		if (difficulty == HARD) {
			Locator.useHardSpeed();
		} else {
			Locator.useEasySpeed();
		}

	}

	public static int getCoins() {
		return coins;
	}

	public static void increaseCoins() {
		coins++;
	}

	public static void addCoins(int coins2) {
		coins += coins2;
		coins = Math.min(coins, 100000);
	}

	public static void removeCoins(int coins2) {
		coins -= coins2;
	}

	public static Color getPlayerTextColor(PLAYER_TYPE pType) {
		switch (pType) {
		case RINGER:
			return COLOR_RINGER_TEXT;
		case APRIL:
			return COLOR_APRIL_TEXT;
		case GEO:
			return COLOR_GEO_TEXT;
		}
		return null;
	}

}
